package com.ln.asset.core.controller;


import com.ln.asset.common.result.R;
import com.ln.asset.core.config.LendNoUtils;
import com.ln.asset.core.mapper.NjSubMapper;
import com.ln.asset.core.pojo.InjOm;
import com.ln.asset.core.pojo.InsAdd;
import com.ln.asset.core.pojo.NjSub;
import com.ln.asset.core.pojo.Om;
import com.ln.asset.core.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OmController
 * @Author LiHaoHao
 * @Date 2022/4/28 16:17
 * Version 1.0
 **/
@RestController
@Api(tags = "这是一个显示申购信息表")
@CrossOrigin
@RequestMapping("/admin/om")
@Slf4j
public class OmController {

    @Resource
    private OmService omService;

    @Resource
    private NjSubService subService;

    @Resource
    private InsAddService insAddService;
    @Resource
    private InjOmService injOmService;
/*
* 全查
*
* */
    @ApiOperation("全部的申购信息展示")
    @GetMapping("/list")
    public R ShowOm() {
        List<Om> list = omService.listOm();
        return R.ok().data("list", list);
    }
    /*
    * 条件查询
    * */
    @ApiOperation("条件查询")
    @PostMapping("/ById/{susubid}/{adepartid}/{sustart}/{supustart}/{subank}/{onesucreatetime}/{twosucreatetime}")
    public R ShowById(
            @ApiParam(value = "申购单号")
            @PathVariable String susubid,
            @ApiParam(value = "所属仓库")
            @PathVariable String adepartid,
            @ApiParam(value = "审批状态")
            @PathVariable int sustart,
            @ApiParam(value = "采购状态")
            @PathVariable int supustart,
            @ApiParam(value = "入库状态")
            @PathVariable int subank,
            @ApiParam(value = "申购开始时间")
            @PathVariable String onesucreatetime,
            @ApiParam(value = "申购结束时间")
            @PathVariable String twosucreatetime
    ) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date onesucreatetimes = sdf.parse(onesucreatetime);
            Date twosucreatetimes = sdf.parse(twosucreatetime);
            List<Om> oms = omService.ShowById(susubid, adepartid, sustart, supustart, subank, onesucreatetimes, twosucreatetimes);
            System.out.println("oms   " + oms.toString());
            return R.ok().data("oms", oms);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }
    @ApiOperation("添加用品信息/添加申购记录")
    @PostMapping("/InsertAdd/{insAdd}/{njSub}")
    public R insertOm(InsAdd insAdd, NjSub njSub) {
        System.out.println("adepartid  "+njSub.getAdepartid());
        String susubids = LendNoUtils.getSGo();
        njSub.setSusubid(susubids);
        njSub.setSustart(1); // 审批状态
        njSub.setSucheckout(0); // 检验状态
        njSub.setSupustart(0); // 采购状态
        njSub.setSubank(0);  // 入库状态
        njSub.setAuserid(1);  // 用户
       njSub.setSubank(0);  // 入库状态
        Integer integer = subService.addAssetcj(njSub);
        if (integer > 0) {
            insAdd.setInjstart(1);
            insAdd.setAtype(0);
            Integer num1 = insAddService.addAssetcj(insAdd);
            if (num1 > 0) {
                InjOm injOm = new InjOm();
                injOm.setInsaddid(insAdd.getAdid());
                injOm.setNjsubid(njSub.getSuid());
                Integer insertadd = injOmService.insertJom(injOm);
                return   R.ok().message("添加成功");
                //采购完成 修改数据字典的数量
//                if (insertadd > 0) {
//                    Dict selectdictByid =dictService.selectDictByIdNuM(insAdd.getAddictid());
//                    Dict dict =new Dict();
//                    dict.setId(insAdd.getAddictid());
//                    Integer num0=selectdictByid.getCounts();
//                    dict.setCounts(num0+insAdd.getAsum());
//                    Integer num2=dictService.updateDictByIds(dict);
//                    if(num2>0){
//                        return R.ok();
//                    }
//                }
            }
        }
        return R.error().message("添加失败");
    }

//    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("批量添加用品信息/添加申购记录")
    @PostMapping("/listInsertAdd/{insAdds}/{njSubs}")
    public R listinsertOm(List<InsAdd> insAdds, NjSub njSubs) {
        System.out.println("adepartid  "+njSubs.getAdepartid());
        String susubids = LendNoUtils.getSGo();
        njSubs.setSusubid(susubids);
        njSubs.setSustart(0); // 审批状态
        njSubs.setSucheckout(0); // 检验状态
        njSubs.setSupustart(0); // 采购状态
        njSubs.setSubank(0);  // 入库状态
        njSubs.setAuserid(1);  // 用户
        njSubs.setSubank(0);  // 入库状态
        Integer integer = subService.addAssetcj(njSubs);
        if (integer > 0) {
            Integer num1 = insAddService.listaddAssetcj(insAdds);
            if (num1 > 0) {
                List<InjOm> injOms =new ArrayList<>();
                for(int i=0;i<insAdds.size();i++){
                    InjOm injOm = new InjOm();
                    injOm.setInsaddid(insAdds.get(i).getAdid());
                    injOm.setNjsubid(njSubs.getSuid());
                    injOms.add(injOm);
                }
                Integer insertadd = injOmService.listinsertJom(injOms);
                return  R.ok().message("批量添加成功");
                //采购完成 修改数据字典的数量
//                if (insertadd > 0) {
//                    Dict selectdictByid =dictService.selectDictByIdNuM(insAdd.getAddictid());
//                    Dict dict =new Dict();
//                    dict.setId(insAdd.getAddictid());
//                    Integer num0=selectdictByid.getCounts();
//                    dict.setCounts(num0+insAdd.getAsum());
//                    Integer num2=dictService.updateDictByIds(dict);
//                    if(num2>0){
//                        return R.ok();
//                    }
//                }
            }
        }
        return  R.error().message("添加失败");
    }

    @Resource
    NjSubMapper njSubMapper ;
    @ApiOperation("修改采购信息")
    @GetMapping("/update1/{id}")
    public R update1(Integer id) {
//        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")format(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1 = sdf.format(new Date());
//        Date date = new Date();
        njSubMapper.update1(id,date1);
        return R.ok().data("list", null);
    }

    @ApiOperation("修改检验信息")
    @GetMapping("/update2/{id}")
    public R update2(Integer id) {
        njSubMapper.update2(id);
        return R.ok().data("list", null);
    }
    @ApiOperation("修改入库信息")
    @GetMapping("/update3/{id}")
    public R update3(Integer id) {
        njSubMapper.update3(id);
        return R.ok().data("list", null);
    }
}
